//
//  MyPlanViewController.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/6/20.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "MyPlanViewController.h"
#import "DaysTrainingPlanViewController.h"
#import "LastingDaysView.h"
#import "TrainingPlanView.h"

@interface MyPlanViewController ()<StartTrainingDelegate,SelectedTrainingDayDelegate>

@property (nonatomic, strong) MyPlanViewModel * viewModel;
@property (nonatomic, strong) LastingDaysView * lastingDaysView;
@property (nonatomic, strong) TrainingPlanView * trainingPlanView;

@end

@implementation MyPlanViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewSetting
{
    [super viewSetting];
    self.title = @"我的计划";
    [self.view addSubview:self.lastingDaysView];
    [self.view addSubview:self.trainingPlanView];
}

- (void)bindViewModel
{
    [super bindViewModel];
    [self.viewModel.getTrainingPlanCommand execute:nil];
    @weakify(self);
    [RACObserve(self.viewModel, trainingPlanData) subscribeNext:^(TrainingPlanData * x) {
        @strongify(self);
        self.lastingDaysView.lastingDayImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"day_%zd",x.duration]];
    }];
}

- (void)makeConstaints
{
    [super makeConstaints];
    WS(weakSelf);
    [self.lastingDaysView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@(screenHeight/3));
    }];
    [self.trainingPlanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.top.equalTo(weakSelf.lastingDaysView.mas_bottom);
    }];
    [self.trainingPlanView.firstDayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((screenHeight/3*2-45-navHeight)/7));
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - SelectedTrainingDayDelegate
- (void)selectedTrainingDayWith:(NSInteger)day
{
    DaysTrainingPlanViewModel * viewModel = [[DaysTrainingPlanViewModel alloc] init];
    viewModel.trainingDay = day;
    viewModel.trainingIdArray = [self.viewModel.trainingPlanData.trainingIdArray objectAtIndex:day-1];
    viewModel.duration = self.viewModel.trainingPlanData.duration;
    DaysTrainingPlanViewController * vc = [[DaysTrainingPlanViewController alloc] initWithViewModel:viewModel];
    [self.navigationController pushViewController:vc
                                         animated:YES];
}

#pragma mark - Getter
- (LastingDaysView *)lastingDaysView
{
    if (!_lastingDaysView) {
        _lastingDaysView = [[LastingDaysView alloc] initWithLastingDay:1];
        _lastingDaysView.startTrainingButton.hidden = YES;
        _lastingDaysView.delegate = self;
    }
    return _lastingDaysView;
}

- (TrainingPlanView *)trainingPlanView
{
    if (!_trainingPlanView) {
        _trainingPlanView = [[TrainingPlanView alloc] init];
        _trainingPlanView.delegate = self;
    }
    return _trainingPlanView;
}

@end
